﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace KingSite.Library.Tools {
    public class CookieManager {

        public static string GetCookieTopDomain() {
            string result = HttpContext.Current.Request.Url.Host;
            string[] strList = result.Split('.');
            int count = strList.Length;
            if (count >= 2) {
                result = string.Format("{0}.{1}", strList[count - 2], strList[count - 1]);
            }
            return result;
        }

        public static void AddCookie(string name, string value) {
            HttpCookie c1 = new HttpCookie(name, value);            
            HttpContext.Current.Response.Cookies.Add(c1);
        }

        public static void AddCookie(string name, string value, string doMain) {
            HttpCookie c1 = new HttpCookie(name, value);
            c1.Domain = doMain;
            HttpContext.Current.Response.Cookies.Add(c1);
        }

        public static void AddCookie(string name, string value, bool SetTopDomain) {
            HttpCookie c1 = new HttpCookie(name, value);
            if (SetTopDomain) {
                c1.Domain = GetCookieTopDomain();
            }
            HttpContext.Current.Response.Cookies.Add(c1);
        }

        public static void AddCookie(string name, string value, bool SetTopDomain, int expiresDays) {
            HttpCookie c1 = new HttpCookie(name, value);
            if (SetTopDomain) {
                c1.Domain = GetCookieTopDomain();
            }
            c1.Expires = DateTime.Now.AddDays(expiresDays);
            HttpContext.Current.Response.Cookies.Add(c1);
        }

        public static void DeleteCookie(string name) {
            HttpCookie c1 = new HttpCookie(name);
            c1.Values.Add(name, null);
            if (c1.Expires != null) {
                DateTime dt = DateTime.Parse("1970-01-01");
                c1.Expires = dt;
            }
            HttpContext.Current.Response.Cookies.Add(c1);
        }

        public static void DeleteCookie(string name, string doMain) {
            HttpCookie c1 = new HttpCookie(name);
            c1.Domain = doMain;
            c1.Values.Add(name, null);
            if (c1.Expires != null) {
                DateTime dt = DateTime.Parse("1970-01-01");
                c1.Expires = dt;
            }
            HttpContext.Current.Response.Cookies.Add(c1);
        }

        public static void DeleteCookie(string name, bool SetTopDomain) {
            HttpCookie c1 = new HttpCookie(name);
            if (SetTopDomain) {
                c1.Domain = GetCookieTopDomain();
            }
            c1.Values.Add(name, null);
            if (c1.Expires != null) {
                DateTime dt = DateTime.Parse("1970-01-01");
                c1.Expires = dt;
            }
            HttpContext.Current.Response.Cookies.Add(c1);
        }

        public static string GetCookie(string name) {
            string result = "";
            HttpCookie c1 = HttpContext.Current.Request.Cookies[name];
            if (c1 != null && !string.IsNullOrEmpty(c1.Value)) {
                result = c1.Value;
            }
            return result;
        }

    }
}
